package com.douban.utils
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	public class Motion extends EventDispatcher
	{
		private var _target:DisplayObject;
		private var proNum:Number;
		private var startNum:Number;
		private var endNum:Number;
		private var actionStr:String;
		private var num1:uint;
		private var num2:uint;
		private var func:Function;
		private var cbfunc:Object;
		
		public function Motion(target:DisplayObject, actionStr:String, func:Function, startNum:Number = 0, endNum:Number = 1, proNum:Number = 4)
		{
			this.actionStr = actionStr;
			this._target = target;
			this.proNum = proNum;
			this.startNum = startNum;
			this.endNum = endNum;
			this.func = func;
			return;
		}
		
		public function play(param1:Function = null):void
		{
			this.stop();
			this.num1 = 0;
			this._target[this.actionStr] = this.startNum;
			this.cbfunc = param1;
			this._target.addEventListener(Event.ENTER_FRAME, enterFrame);
			return;
		}
		
		private function enterFrame(e:Event):void
		{
			this.num1++;
			var _loc_3:Number = this.proNum;
			var _loc_4:Number = this.startNum;
			var _loc_5:Number = this.endNum - this.startNum;
			this._target[this.actionStr] = this.func(this.num1++, _loc_4, _loc_5, _loc_3);
			if (num1 > proNum)
			{
				this.stop();
				this._target[this.actionStr] = this.endNum;
			}
			return;
		}
		
		public function back(param1:Function = null):void
		{
			this.stop();
			this.num2 = 0;
			this._target[this.actionStr] = this.endNum;
			this.cbfunc = param1;
			this._target.addEventListener(Event.ENTER_FRAME, this.backEnterFrame);
			return;
		}
		
		private function backEnterFrame(e:Event):void
		{
			this.num2++;
			this._target[this.actionStr] = this.func(this.num2++, endNum, (this.startNum - this.endNum), proNum);
			if (num2 > proNum)
			{
				this.stop();
				this._target[this.actionStr] = this.startNum;
			}
			return;
		}
		
		public function stop():void
		{
			this._target.removeEventListener(Event.ENTER_FRAME, enterFrame);
			this._target.removeEventListener(Event.ENTER_FRAME, backEnterFrame);
			dispatchEvent(new Event("stop"));
			if (this.cbfunc)
			{
				this.cbfunc();
				this.cbfunc = null;
			}
			return;
		}
	}
}